package com.spring.api.web.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Maps;
import com.spera.genutil.SpUtil;
import com.spera.genutil.TokenUtil;
import com.spring.api.web.model.VideoMonitorOpen;
import com.spring.api.web.service.EnvizApiService;
import com.spring.nbrbt.tools.dateutil.UDate;
import com.spring.nbrbt.tools.otherutil.NbUtil;
import com.spring.security.web.controller.BaseController;
import com.spring.security.web.dao.JedisCache;
import com.spring.security.web.model.BaseResult;
import com.spring.tools.MyTools;
import com.sso.api.web.dao.TracelogboMapper;
import com.sso.api.web.model.TracelogboWithBLOBs;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;

@Controller
@RequestMapping(value = "/ezviz")
public class EnvizApiController extends BaseController{
	
	@Resource private EnvizApiService envizApiService;
	@Resource private JedisCache jedisCache;
	
	@Resource private TracelogboMapper tracelogboMapper;
	
	/**
	 * 判断账号查看摄像头权限
	 * @param token
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
    @RequestMapping(value="/accessToken", method = RequestMethod.GET)
    @ApiOperation(value = "萤石云accessToken", httpMethod = "GET", response = Object.class, notes = "萤石云accessToken",produces = "application/json; charset=utf-8")
    public Object accessToken(@ApiParam(required = true, name = "token", value = "token") @RequestParam String token) throws Exception{
		BaseResult ret = SpUtil.checkTokenNew(token, jedisCache);
		if("1".equalsIgnoreCase(ret.getStatus())){//登录状态
			//boolean checkTime = checkVideoList();
			int remainTime=envizApiService.getRemainTime();
			if(remainTime<=0){
				ret.setStatus("0").setMsg("非开放时段");
				ret.setDataset(new ArrayList<Map<String, Object>>());
				return ret;
			}			
			Map<String,Object> parm = Maps.newHashMap();
			parm.put("c_family_id", jedisCache.hget(token, TokenUtil.FAMILY_ID));
			String retData = envizApiService.accessToken(parm);
			if(StringUtils.isBlank(retData)){
				ret.setStatus("0");
				ret.setMsg("不可用");
				return ret;
			}
			ret.setDataset(retData);
		}
        return ret;
    }
	
	/**
	 * 获取摄像头列表
	 * @param token
	 * @param kidsPark
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
    @RequestMapping(value="/ezvizList", method = RequestMethod.GET)
    @ApiOperation(value = "萤石云列表", httpMethod = "GET", response = Object.class, notes = "萤石云列表",produces = "application/json; charset=utf-8")
    public Object ezvizList(@ApiParam(required = true, name = "token", value = "token") @RequestParam String token,
    		@ApiParam(required = true, name = "kidsPark", value = "乐园门店编号") @RequestParam String kidsPark,
		    @ApiParam(required = true, name = "ezvizId", value = "ezvizId") @RequestParam(required=false) String ezvizId,
		    HttpServletRequest request) throws Exception{
			
		if("47a32895-38fb-4d9d-8f49-4c29e82adade".equals(kidsPark)){
			kidsPark = "04428ed2-a555-11e5-8ece-1051721d3a6c";
		}

		BaseResult ret = SpUtil.checkTokenNew(token, jedisCache);
		if("1".equalsIgnoreCase(ret.getStatus())){//登录状态

			int remainTime=envizApiService.getRemainTime();
			if(remainTime<=0){
				ret.setStatus("0").setMsg("非开放时段");
				ret.setDataset(new ArrayList<Map<String, Object>>());
				return ret;
			}	
			try{
				Map<String,Object> parm = Maps.newHashMap();
				parm.put("kidsPark", kidsPark);
				parm.put("c_family_id", jedisCache.hget(token, TokenUtil.FAMILY_ID));
				parm.put("ezvizId", ezvizId);				
				//parm.put("c_family_id", "681177ac-08b5-4125-80fd-58d22bb16087");

				List<Map<String, Object>> retData = envizApiService.ezvizs(parm);
				if(retData==null){
					ret.setStatus("0");
					ret.setMsg("无权限");
					retData=new ArrayList<Map<String, Object>>();
					ret.setDataset(retData);
					return ret;
				}
				ret.setDataset(retData);
				if(ezvizId!=null){									
					envizApiService.insertLog(ezvizId,token, request);
				}
			}catch(Exception ex){
				ex.printStackTrace();
				ret.setDataset(new ArrayList<Map<String, Object>>());
			}
		}
        return ret;
    }
	
	public boolean checkVideoList(){
		List<VideoMonitorOpen> list = envizApiService.checkVideoList(null);
		Date now = new Date();
		//先检测有无特殊日子
		for(VideoMonitorOpen one:list){
			if(1==one.getIsSpecial()&&
					NbUtil.isNotNull(one.getSpecialBeginDate())&&
					NbUtil.isNotNull(one.getSpecialEndDate())){
				//判断是否在特殊日期时间范围内
				if(now.getTime()>one.getSpecialBeginDate().getTime()&&
						now.getTime()<one.getSpecialEndDate().getTime()){
					return true;
				}
				
			}
		}
		//如果没有特殊日期，则判断正常日期
		Date bt = null;
		Date et = null;
		for(VideoMonitorOpen one:list){
			if(0==one.getIsSpecial()&&
					NbUtil.isNotNull(one.getBeginTime())&&
					NbUtil.isNotNull(one.getEndTime())){
				//判断时间范围
				bt = UDate.getDateByHAndM(one.getBeginTime());
				et = UDate.getDateByHAndM(one.getEndTime());
				if(bt!=null&&et!=null){
					if(now.getTime()>bt.getTime()&&
							now.getTime()<et.getTime()){
						return true;
					}
				}
			}
		}
		return false;
	}
}
